/*
 * Project it.battlehorse.rcp.tools.dbbase
 * Created on May 14, 2006
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */ 
package it.battlehorse.rcp.tools.dbbase;

import it.battlehorse.rcp.tools.dbbase.internal.DbManager;

import java.util.List;

import javax.sql.DataSource;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.Viewer;

/**
 * Provides a standard interface to manage databases, datasources and the active connections.
 * 
 * @author battlehorse
 * @since May 14, 2006
 */
public interface IDbManager {

	/**
	 * Returns the list of the available Database configurations.
	 * 
	 * @return the list of the available Database configurations, or {@code null} if
	 * 		no database is available.
	 * @see DbConfig
	 */
	public List<DbConfig> getDbConfigList();

	/**
	 * Searches the {@code DbConfig} identified by {@code name} among the available
	 * databases configurations.
	 * 
	 * @param name the name of the searched database configuration
	 * @return the associated DbConfig, or {@code null} if nothing was found.
	 */
	public DbConfig findDbConfig(String name);

	/**
	 * Searches the {@code DatasourceConfig} identified by {@code name} among the available
	 * databases configurations.
	 * 
	 * @param name the name of the searched database configuration
	 * @return the associated DatasourceConfig, or {@code null} if nothing was found.
	 */
	public DatasourceConfig findDatasourceConfig(String name);

	/**
	 * Tells if all the registered datasources are idle, i.e. no connection is being used
	 * 
	 * @return {@code true} if all the datasources are idle, {@code false} otherwise.
	 */
	public boolean isIdle();

	/**
	 * Loads the configured datasources from the preferences store
	 * 
	 * @param store from the preferences store
	 * @return the configured datasources
	 */
	public List<DatasourceConfig> loadDatasourceConfig(IPreferenceStore store);

	/**
	 * Stores the given datasources configuration into the preferences store
	 * 
	 * @param store the preferences store
	 * @param l the list of the configured datasources
	 */
	public void storeDatasourceConfig(IPreferenceStore store,
			List<DatasourceConfig> l);

	/**
	 * Returns a datasource configured with the given configuration parameters
	 * 
	 * @param ds the datasource configuration
	 * @return a configured DataSource	 
	 */
	public DataSource getDataSource(DatasourceConfig ds);

	/**
	 * Returns a datasource configured with the given configuration parameters
	 * 
	 * @param ds the datasource configuration
	 * @param storeConfiguration defines if the returned datasource has to be stored into
	 * 		the internal registry. If the datasource is not stored, the caller MUST close
	 * 		it once it has finished working with it
	 * @return a configured DataSource	 
	 */
	public DataSource getDataSource(DatasourceConfig ds,
			boolean storeConfiguration);

	/**
	 * Add a viewer to the list of viewers which need to be notified whenever
	 * the datasources list changes. As soon as the viewer is added, it's {@code input}
	 * property is set with the up-to-date list of datasources. If possible, implementations
	 * should try to set valid content and label providers on the viewer.
	 * 
	 * @param v the viewer
	 */
	public void addDatasourceViewer(Viewer v);

	/**
	 * Removes a registered datasource viewer.
	 * 
	 * @see DbManager#addDatasourceViewer(Viewer)
	 * @param v the viewer
	 */
	public void removeDatasourceViewer(Viewer v);

	/**
	 * Closes all the open datasources and release the associated resources
	 *
	 */
	public void shutdown();

}